package com.hxkj.controller;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.ArticleCollectionServiceClient;
import com.hxkj.domain.po.*;
import com.hxkj.entity.vo.UserArticleDetail;
import com.hxkj.service.ArticleService;
import com.hxkj.service.TokenService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.Callable;

/**
 * created by kezhijie on 2018/9/4 17:29.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleCollectionServiceClient articleCollectionServiceClient;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ArticleService articleService;

    /**
     * 取消文章收藏
     * @param userArticle
     * @throws Exception
     */
    @RequestMapping(value = "CollectArticle", method = RequestMethod.POST)
    public int CollectArticle(@ModelAttribute UserArticle userArticle, HttpServletRequest request) throws Exception {
        ResultMessageStruct resultMessageStruct ;

        String accessToken = request.getHeader("accessToken");
        userArticle.setUserId(getUserIdByToken(accessToken));

        if (StringUtils.isEmpty(userArticle.getArticleId()))
            throw new CustomException("文章编号不能为空");

        if("0".equals(userArticle.getType())){//文章是否被收藏
            return articleCollectionServiceClient.ifCollectArticle(userArticle);
        }else if("1".equals(userArticle.getType())){//文章收藏
            if(userArticle.getSiteId() == null){
                throw new CustomMessageException("站点编号不能为空");
            }
            resultMessageStruct = articleCollectionServiceClient.collectArticle(userArticle);
            if (resultMessageStruct.getCode() != 200)
                throw new CustomMessageException(resultMessageStruct.getMessage());

            return 1;
        }else{//取消收藏
            resultMessageStruct = articleCollectionServiceClient.cancelCollectArticle(userArticle);
            if (resultMessageStruct.getCode() != 200)
                throw new CustomMessageException(resultMessageStruct.getMessage());

            return 0;
        }

    }

    @PostMapping("getCollectionList")
    public PageInfo<UserArticle> getCollectionList(@ModelAttribute PageTo pageParam, HttpServletRequest request) throws Exception {
        String accessToken = request.getHeader("accessToken");

        UserArticle userArticle = new UserArticle();
        userArticle.setUserId(getUserIdByToken(accessToken));

        ParameterDataStruct<UserArticle> parameterDataStruct = new ParameterDataStruct<>();
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageSize(10);
            pageParam.setPageNum(1);
        }
        parameterDataStruct.setPageTo(pageParam);
        parameterDataStruct.setData(userArticle);

        return articleCollectionServiceClient.getCollectionList(parameterDataStruct);
    }

    /**
     * 最新资讯分页列表
     * @param siteId
     * @param pageParams
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getNewestArticleList")
    public Callable<PageInfo<Article>> getNewestArticleList(@ModelAttribute Long siteId, @ModelAttribute PageTo pageParams)
            throws Exception{
        return () -> articleService.getNewestArticle(siteId, pageParams);
    }

    private String getUserIdByToken(String accessToken) throws Exception{
        Token token = tokenService.validate(accessToken).getData();
        if(StringUtils.isEmpty(token.getUserId())){
            throw new CustomMessageException("当前登录用户不存在");
        }
        return token.getUserId();
    }

    private UserArticleDetail buildUserArticleDetail(UserArticle userArticle) {
        UserArticleDetail userArticleDetail = new UserArticleDetail();
        userArticleDetail.setCreateTime(userArticle.getCreateTime());
        userArticleDetail.setDelTag(userArticle.getDelTag());
        userArticleDetail.setId(userArticle.getId());
        userArticleDetail.setSiteId(userArticle.getSiteId());
        userArticleDetail.setTitle(userArticle.getTitle());
        userArticleDetail.setUpdateTime(userArticle.getUpdateTime());
        userArticleDetail.setUrl(userArticle.getUrl());
        userArticleDetail.setUserId(userArticle.getUserId());
        return userArticleDetail;
    }


}
